<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/com/acc_conn.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/com/rpc_com.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/com/fea/accMisc.php';

//流水号,账套唯一. 凭证作废时,流水号不收回
function getFlowWaterNum() {
    $pdb = new acc_DB();
    $ssql = "select flowNo from daybook_main order by flowNo DESC limit 1";

    $fnb = $pdb->qw($ssql, "flowNo");
    if (null == $fnb){
        return 1;
    }
    return $fnb + 1;
}

function getVochNo($vochZiId, $u_year = null, $u_month = null) {//凭证号,月度+凭证字内唯一,连续. 凭证作废时凭证号不收回
    $pdb = new acc_DB();
    if(!isset($u_year)) {
        $u_year = $pdb->qw("select c.value from acc_confs as c where item='cur_acc_year'", "value");
    }
    if(!isset($u_month)) {
        $u_month = $pdb->qw("select c.value from acc_confs as c where item='cur_acc_month'", "value");
    }
    
    $vcn = $pdb->qw("select vochNo from daybook_main where Date_y = '" .$u_year . "' and Date_m='". $u_month . "' and vochZiId='" . $vochZiId . "' order by vochNo DESC limit 1", "vochNo");

    if (null == $vcn){
        return 1;
    }
    return $vcn + 1;
}

function getVochDate(){
    $pdb = new acc_DB();
    $ssql = "select c.value from acc_confs as c where item='cur_acc_year'";
    $y = $pdb->qw($ssql, "value");
    $ssql = "select c.value from acc_confs as c where item='cur_acc_month'";
    $m = $pdb->qw($ssql, "value");
    $ssql = "select Date_y, Date_m, Date_d from daybook_main where Date_y = '" . $y ."' and Date_m = '" . $m . "' order by Date_y, Date_m, Date_d DESC limit 1;";
    $rs = $pdb->q($ssql);
    $row = $rs->fetchArray(SQLITE3_ASSOC);

    if (empty($row)) {
        $d = mktime(1, 1, 1, $m, 1, $y);
        return date("Y-m-d", $d);
    }
    return sprintf("%04d-%02d-%02d", $row["Date_y"], $row["Date_m"], $row["Date_d"]);
}

function dorpc_voch_new($data, &$res){
    $vochziid = $data["vochziid"];
    $res["FlowWaterNum"] = getFlowWaterNum();
    $res["VochDate"] = getVochDate();
    $res["VochNo"] = getVochNo($vochziid);
    set_res_OK($res, "foo");
}

function dorpc_acc_voch_save($data, &$res) {
    if(!isset($data["jours"])) {
        set_res_NG($res, "no data to saving.");
        return;
    }
    $cnt = count($data["jours"]);
    if(0 == $cnt) {
        set_res_NG($res, "no data to saving(count:0).");
        return;
    }
    for ($i = 0; $i < $cnt; ++$i) {
        if(!empty($data["jours"][$i]["accCloseDate"])) {
            $iDate = 0;
            if(convert_strDate_to_iDate($data["jours"][$i]["accCloseDate"], $iDate)) {
                set_res_grid_NG($res, null, "row" . $i . " Date Error", "id_new_voch_grid", $i, "accCloseDate");
                return;
            } 
        }
    }
    
    $pdb = new acc_DB();
    $nowFlowNum = getFlowWaterNum();
    
    if(convert_strDates_to_ymdDate($data["date"], $y, $m, $d)) {
        set_res_NG($res, null, $data["date"] . " invalid.", "id_new_voch_datepicker");
        return;
    }
    $nowVochNo = getVochNo($data["vochZiID"], $y, $m);
    
    $ssql = "insert into daybook_main(flowNo, iDate, Date_y, Date_m, Date_d, vochZiId, vochNo, attachCnt, " .
    "makerUid, reviwerUid, bookerUid, accManagerUid) values(";
    $ssql .= $nowFlowNum . ", ";
    $ssql .= ($y * 12 * 31 + $m * 31 + $d ). ", " . $y . ", " . $m . ", " . $d . ", ";
    $ssql .= $data["vochZiID"] . ", "; //抛来的即是ziID,直接使用
    $ssql .= $nowVochNo . ", ";
    $ssql .= $data["vochAttachCnt"] . ", " .
    $data["id_acc_maker"] . ", -1, -1, -1)";
    
    if(!($pdb->q($ssql))){
        set_res_NG($res, $ssql);
        return;
    }
    $mainID = $pdb->qw("select id from daybook_main order by id desc limit 1", "id");

    for ($i = 0; $i < $cnt; ++$i) {
        $ssql = "insert into daybook_sub(input_idx, mainId, summary, accId, auxSaleManId, " . 
            "auxPrj1Id, auxPrj2Id, auxPrj3Id, auxCurrentId, accWayId, accCloseNo, accCloseDate, " .
            "de, cr) values(";
        $ssql .= $i + 1 . ", '"; //input_idx
        $ssql .= $mainID . "', '"; //mainId
        $ssql .= $data["jours"][$i]["zhaiyao"] . "', '"; //summary
        $ssql .= $data["jours"][$i]["kemuId"] . "', '"; //account.id
        $ssql .= $data["jours"][$i]["auxSaleManId"] . "', '"; //核算业务员
        $ssql .= $data["jours"][$i]["auxPrj1Id"] . "', '"; //核算项目1 id
        $ssql .= $data["jours"][$i]["auxPrj2Id"] . "', '"; //核算项目2 id
        $ssql .= $data["jours"][$i]["auxPrj3Id"] . "', '"; //核算项目3 id
        $ssql .= $data["jours"][$i]["auxCurrentId"] . "', '"; //往来 id
        $ssql .= $data["jours"][$i]["accWayId"] . "', '";//(empty($data["jours"][$i]["accWayId"]) ? "200" : $data["jours"][$i]["accWayId"]) . "', '"; //结算方式 id
        $ssql .= $data["jours"][$i]["accCloseNo"] . "', '"; //结算号
        if(empty($data["jours"][$i]["accCloseDate"])) { //结算日期 iDate
            $ssql .= -1 . "', '";
        } else {
            $iDate = 0;
            convert_strDate_to_iDate($data["jours"][$i]["accCloseDate"], $iDate);
            $ssql .=  $iDate . "', '";
        }
        $ssql .= (empty($data["jours"][$i]["de"]) ? 0 : $data["jours"][$i]["de"]) . "', '";
        $ssql .= (empty($data["jours"][$i]["cr"]) ? 0 : $data["jours"][$i]["cr"]) . "'";

        $ssql .= ")";
        if (!$pdb->q($ssql)) {
            set_res_NG($res, $ssql);
            return;
        }
    }
    $pdb->d();

    $res["FlowWaterNum"] = $nowFlowNum + 1; //新的流水号
    $res["VochNo"]  = $nowVochNo + 1;  //新的凭证号
    $res["VochDate"] = $data["date"];
    set_res_OK($res, $ssql);
}
?>

